<extend name="Public/base"/>
<block name="style" >
    <link rel="stylesheet" href="__STATIC__/styles/vendors/bootstrap-table.css">
    <link rel="stylesheet" href="__STATIC__/styles/daily_manage.css">
    <link rel="stylesheet" href="__STATIC__/styles/vendors/bootstrap-select.min.css">
    <link rel="stylesheet" href="__STATIC__/plugins/dropper/jquery.fs.dropper.min.css">
    <link rel="stylesheet" href="__STATIC__/styles/vendors/bootstrap-datetimepicker.min.css">
    <style>
        table {table-layout:fixed;}
        td.overhide {
            width: 100%;
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
        }
        textarea.form-control.textarea_ct {
            min-height: 300px;
        }
    </style>
</block>
<block name="content">

    <div class="bs-example bs-example-tabs nav-tabs-custom" data-example-id="togglable-tabs">
        <ul id="myTabs" class="nav nav-tabs" role="tablist">
            <li role="presentation" class="active"><a href="#work" id="work-tab" role="tab" data-toggle="tab" aria-controls="work" aria-expanded="true">工作安排</a></li>
            <li role="presentation" class=""><a href="#arrange" role="tab" id="arrange-tab" data-toggle="tab" aria-controls="arrange" aria-expanded="false">值班安排</a></li>
        </ul>
        <div id="myTabContent" class="tab-content" style="max-height: 100%">
            <div role="tabpanel" class="tab-pane fade active in" id="work" aria-labelledby="work-tab">
                <div id="workToolbar">
                    <button id="removeWork" class="btn btn-danger btn-flat" disabled>
                        <i class="glyphicon glyphicon-remove"></i>删除
                    </button>

                    <button id="addWork" class="btn btn-primary btn-flat">
                        添加工作安排
                    </button>
                </div>

                <table id="workTable" data-toolbar="#workToolbar">
                </table>
            </div>
            <div role="tabpanel" class="tab-pane fade" id="arrange" aria-labelledby="arrange-tab">
                <div id="toolbar">
                    <button id="remove" class="btn btn-danger btn-flat" disabled>
                        <i class="glyphicon glyphicon-remove"></i>删除
                    </button>

                    <button id="import" class="btn btn-primary btn-flat">
                        导入数据
                    </button>
                </div>

                <table id="table" data-toolbar="#toolbar">
                </table>
            </div>
        </div>
    </div>




    <div class="modal fade" id="import_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="container-fluid">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                        <h4 class="modal-title">导入数据</h4>
                    </div>
                    <form id="importForm" enctype="multipart/form-data" action="{:U('import')}" method="post">
                        <div class="modal-body">
                            <div class="control-group row">
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 left">
                                    <label class="control-label" >文件:</label>
                                </div>
                                <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9 right">
                                    <div class="controls">
                                        <input type="file" id="file_import" class="form-control" name="file_import">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" id="modelLoad" class="btn btn-primary btn-flat pull-left">模版下载</button>
                            <button type="submit" id="importButton" class="btn btn-danger">导入</button>
                            <button type="button" class="btn btn-default btn-flat" data-dismiss="modal">关闭</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <div class="modal fade" id="confirm_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">确认</h4>
                </div>
                <div class="modal-body">
                    确定要删除吗?
                </div>
                <div class="modal-footer">
                    <button type="button" id="confirmDel" class="btn btn-danger btn-flat">删除</button>
                    <button type="button" class="btn btn-default btn-flat" data-dismiss="modal">取消</button>
                </div>
            </div>
        </div>
    </div>
    <div class="modal fade" id="complex_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel2">
        <div class="modal-dialog" role="document">
            <form id="addForm" class="modal-content" action="{:U('addWorkArrange')}" method="post">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">工作安排</h4>
                </div>
                <div class="modal-body">


                    <div class="control-group row">
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 left">
                            <label class="control-label" for="content">工作说明:</label>
                        </div>
                        <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9 right">
                            <div class="controls">
                                <textarea class="form-control" id="content" name="content" rows="10"></textarea>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary">确定</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                </div>
            </form>
        </div>
    </div>

<!-- 查看 & 修改 -->
    <div class="modal fade" id="update_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel2">
        <div class="modal-dialog" role="document">
            <form id="updateForm" class="modal-content" action="{:U('pudateContent')}" method="post">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">内容详情</h4>
                </div>
                <div class="modal-body">
                    <div class="control-group row look">
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 left">
                            <label class="control-label" for="content">添加日期:</label>
                        </div>
                        <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9 right">
                            <div class="controls">
                                <p  id="send_time" name="send_time_no" rows="10"></p>
                            </div>
                        </div>
                    </div>
                    <br/>
                    <div class="control-group row">
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 left">
                            <label class="control-label" for="content">工作内容:</label>
                        </div>
                        <div class="look col-lg-9 col-md-9 col-sm-9 col-xs-9 right">
                            <div class="controls">
                                <p class="" id="content" rows="10"></p>
                            </div>
                        </div>
                        <div class="updateLook col-lg-9 col-md-9 col-sm-9 col-xs-9 right">
                            <div class="controls">
                                <textarea class="form-control textarea_ct"  name="content" rows="10"></textarea>
                            </div>
                        </div>
                    </div>
                    <input type="hidden" name="work_arrange_id" id="work_arrange_id">
                </div>
                <div class="modal-footer">
                    <button type="submit" class="updateLook btn btn-primary">修改</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                </div>
            </form>
        </div>
    </div>
</block>

<block name="script">
    <script src="__STATIC__/scripts/vendors/bootstrap-select.min.js"></script>
    <script src="__STATIC__/scripts/vendors/bootstrap-table.js"></script>
    <script src="__STATIC__/plugins/dropper/jquery.fs.dropper.min.js"></script>
    <script src="__STATIC__/scripts/vendors/bootstrap-treeview.min.js"></script>
    <script src="__STATIC__/scripts/vendors/moment.min.js"></script>
    <script src="__STATIC__/scripts/vendors/bootstrap-datetimepicker.min.js"></script>
    <script src="__STATIC__/scripts/vendors/jquery.form.min.js"></script>
    <script>
        var $table = $('#table');
        var $workTable = $('#workTable');
        var $tree = $('#tree');
        var $remove = $('#remove');
        var $removeWork = $('#removeWork');
        // 查看修改
        var $update_modal = $('#update_modal');
        var orgData;
        var org_id = 0;
        var role_id = 0;
        var $complexModal = $('#complex_modal');
        var $importModal = $('#import_modal');
        var $confirmDel = $('#confirmDel');
        var delArrange = true;

        var TableInit = function(){
            var oTableInit = new Object();

            oTableInit.selections = [];

            oTableInit.workSelections = [];

            oTableInit.init = function(){
                $table.bootstrapTable({
                    formatLoadingMessage: function () {
                        return '正在努力地加载数据中，请稍候……';
                    },
                    formatRecordsPerPage: function (pageNumber) {
                        return '每页显示 ' + pageNumber + ' 条记录';
                    },
                    formatShowingRows: function (pageFrom, pageTo, totalRows) {
                        return '显示第 ' + pageFrom + ' 到第 ' + pageTo + ' 条记录，总共 ' + totalRows + ' 条记录';
                    },
                    formatSearch: function () {
                        return '搜索';
                    },
                    formatNoMatches: function () {
                        return '没有找到匹配的记录';
                    },
                    formatPaginationSwitch: function () {
                        return '隐藏/显示分页';
                    },
                    formatRefresh: function () {
                        return '刷新';
                    },
                    formatToggle: function () {
                        return '切换';
                    },
                    formatColumns: function () {
                        return '列';
                    },
                    url:"{:U('arrangeList')}",
                    method:'get',
                    striped:true,
                    cache:false,
                    pagination:true,
                    sortable:false,
                    sortOrder:'asc',
                    sidePagination:'server',
                    queryParams:oTableInit.queryParams,
                    pageNumber:1,
                    pageSize:10,
                    pageList:[10,20,50],
                    height:getHeight(),
//                    uniqueId:'work_arrange_id',
//                    search:true,


                    columns:[
                        {
                            checkbox:true,
                            align:'center'
                        },
//                        {
//                            field:'work_arrange_id',
//                            title:'序号',
//                            visible:false
//                        },
                        {
                            field:'duty_date',
                            title:'日期',
                            width:200,
                            formatter:function (value, row){
                                if(row.isHoliday == 1){
                                    return value + ' ' + row.weekday + ' <img width="24px" src="__STATIC__/images/holiday.png">';
                                }else{
                                    return value + ' ' + row.weekday ;
                                }

                            }

                        },
                        {
                            field:'items',
                            title:'排班',
                            formatter:function (value, row, index) {
                                var info = '';
                                for(var i in value){
                                    info += '<p>' + value[i].duty_type_name + ':' + value[i].duty_user + '</p>';
                                }
                                return info;
                            }
                        }
                    ]
                });
                $workTable.bootstrapTable({
                    formatLoadingMessage: function () {
                        return '正在努力地加载数据中，请稍候……';
                    },
                    formatRecordsPerPage: function (pageNumber) {
                        return '每页显示 ' + pageNumber + ' 条记录';
                    },
                    formatShowingRows: function (pageFrom, pageTo, totalRows) {
                        return '显示第 ' + pageFrom + ' 到第 ' + pageTo + ' 条记录，总共 ' + totalRows + ' 条记录';
                    },
                    formatSearch: function () {
                        return '搜索';
                    },
                    formatNoMatches: function () {
                        return '没有找到匹配的记录';
                    },
                    formatPaginationSwitch: function () {
                        return '隐藏/显示分页';
                    },
                    formatRefresh: function () {
                        return '刷新';
                    },
                    formatToggle: function () {
                        return '切换';
                    },
                    formatColumns: function () {
                        return '列';
                    },
                    url:"{:U('workList')}",
                    method:'get',
                    striped:true,
                    cache:false,
                    pagination:true,
                    sortable:true,
                    sortName:'send_time',
                    sortOrder:'desc',
                    sidePagination:'server',
                    queryParams:oTableInit.workQueryParams,
                    pageNumber:1,
                    pageSize:10,
                    pageList:[10,20,50],
                    uniqueId:'work_arrange_id',
//                    search:true,
                    height:getHeight(),

                    columns:[
                        {
                            checkbox:true,
                            align:'center'
                        },
                        {
                            field:'work_arrange_id',
                            title:'序号',
                            visible:false,
                            align:'center'
                        },
                        {
                            field:'send_time',
                            title:'添加日期',
                            width:200,
                            align:'center',
                            sortable:true,
                        },
                        {
                            field:'content',
                            title:'工作内容',
                            class:'overhide',
                            align:'center',
                            formatter:function(value){
                                return '<a href="javascript: void 0" class="content">'+ value +'</a>';
                            },
                            events:operateEvents
                        },
                        {
                            field:'operate',
                            title:'操作',
                            align:'center',
                            width:200,
                            formatter:function(value){
                                return "<button class='btn btn-primary btn-flat update_data'>修改</button>";
                            },
                            events:operateEvents
                        }
                    ]
                });



                $(window).resize(function () {
                    $table.bootstrapTable('resetView',{height: getHeight()});
                });

//                $('#mytabcontent').resize(function () {
//                    $table.bootstrapTable('resetView',{height: getHeight()});
//                });

                $table.on('load-success.bs.table',function () {
                    setTimeout(function () {
                        $table.bootstrapTable('resetView',{height: getHeight() - 50});
                    },1500);
//                    $table.bootstrapTable('resetView', {height:getHeight()});
                });

                $table.on('check.bs.table uncheck.bs.table ' +
                        'check-all.bs.table uncheck-all.bs.table', function () {
                    $('#remove').prop('disabled', !$table.bootstrapTable('getSelections').length);

                    // save your data, here just save the current page
                    oTableInit.selections = oTableInit.getIdSelections();
                    // push or splice the selections if you want to save all data selections
                });



                $workTable.on('check.bs.table uncheck.bs.table ' +
                    'check-all.bs.table uncheck-all.bs.table', function () {
                    $('#removeWork').prop('disabled', !$workTable.bootstrapTable('getSelections').length);

                    // save your data, here just save the current page
                    oTableInit.workSelections = oTableInit.getWorkIdSelections();
                    // push or splice the selections if you want to save all data selections
                });



            };

            oTableInit.queryParams = function(params){
                var temp = {
                    limit:params.limit,
                    offset:params.offset,
                    search:params.search,
                    searchSelect:$('#searchSelect').val()
                };
                return temp;
            };

            oTableInit.workQueryParams = function(params){
                var temp = {
                    limit:params.limit,
                    offset:params.offset,
                    rder:this.sortOrder
                };
                return temp;
            };


            oTableInit.getIdSelections = function(){
                return $.map($table.bootstrapTable('getSelections'), function (row) {
                    return row.duty_date;
                })
            };
            oTableInit.getWorkIdSelections = function(){
                return $.map($workTable.bootstrapTable('getSelections'), function (row) {
                    return row.work_arrange_id;
                })
            };

            return oTableInit;
        };
        function initComplexModal(data){
            for(var p in data){
                if(typeof(data[p]) != "function"){
                    $update_modal.find('#' + p).text(data[p]) ;
                }
            }
        }
        window.operateEvents = {
            'click .content':function(e, value, row, index){
                $('.look').show();
                $('.updateLook').hide();
                $update_modal.modal('show');
                initComplexModal(row);
            },
            'click .update_data':function(e,value,row,index){
                $('.look').hide();
                $('.updateLook').show();
                $update_modal.modal('show');
                $('#send_time').text(row.send_time);
                $('.textarea_ct').val(row.content);
                $('#work_arrange_id').val(row.work_arrange_id);
            }
        };
        function initValidate(){
            $('#addForm').validate({
                debug:true,
                submitHandler:function (form) {
                    var url = $(form).prop('action');
                    var postData = $(form).serialize();
                    $.post(url,postData, function(data){
                        if(data.status == 1){
                            layer.msg('编辑成功');
                            form.reset();

                            $complexModal.modal('hide');
                            $workTable.bootstrapTable('refresh');
                        }else{
                            layer.msg(data.msg);
                        }
                    });
                },
                rules:{
                    content:'required'
                },
                messages:{
                    content:'请填写内容'
                }
            });
            // 修改
            $('#updateForm').validate({
                debug:true,
                submitHandler:function(form){
                    var url = $(form).prop('action');
                    var postData = $(form).serialize();
                    layer.confirm('是否修改工作内容?', {
                        title: '提示',
                      btn: ['是','否'] //按钮
                    }, function(){
                        $.post(url,postData,function(data){
                            if (data.status == 1) {
                                layer.msg('修改成功');
                                $update_modal.modal('hide');
                                $workTable.bootstrapTable('refresh');
                            }else{
                                layer.msg(data.msg);
                            }
                        });
                    }, function(){
                    });
                },
                rules:{
                    content:'required'
                },
                messages:{
                    content:'请填写内容'
                }
            });


            $('#importForm').validate({
                debug:true,
                submitHandler:function (form) {
                    var url = $(form).prop('action');
                    $(form).ajaxSubmit({
                        url:url,
                        success:function(data){
                            if(data.status == 1){
                                var info = '<p>导入结果：</p>';
                                info = info + '<p>成功导入' + data.data.success + '条</p>';
                                info = info + '<p>失败' + data.data.failed + '条</p>';
                                if(data.data.errLines && data.data.errLines.length>0){
                                    for (var i = 0; i < data.data.errLines.length; i++){
                                        info = info + '<p>第' + data.data.errLines[i] + '条导入失败</p>';
                                    }
                                }
                                layer.alert(info, {
                                    skin: 'layui-layer-lan' //样式类名
                                    ,closeBtn: 0,
                                    anim: 1
                                });
                                $importModal.modal('hide');
                                $table.bootstrapTable('refresh');
                            }else{
                                layer.msg(data.msg);
                            }
                        }
                    })

                },
                rules:{
                    file_import:'required'
                },
                messages:{
                    file_import:'请选择上传文件'
                }
            });


        }

        function getHeight() {
            return $("#right").height() - 30;
        }

        $(function () {

            initValidate();

            var oTable = new TableInit();
            oTable.init();

            $remove.click(function () {
                $("#confirm_modal").modal('show');
                delArrange = true;
            });

            $removeWork.click(function () {
                $("#confirm_modal").modal('show');
                delArrange = false;
            });

            $('#searchSelect').on('changed.bs.select', function () {
                $table.bootstrapTable('refresh');
            });

            $('#addWork').click(function () {
                $complexModal.modal('show');
            });


            $('#import').click(function () {
                $importModal.modal('show');
            });
            // 下载模版
            $('#modelLoad').click(function(){
                var root = "{:C('MODEL_LOAD_DOC_DUTY')}";
                window.location.href = root;
            });
            $confirmDel.click(function () {
                $("#confirm_modal").modal('hide');
                if(delArrange){
                    $.post({
                        url:"{:U('delArrange')}",
                        data:{
                            selectedIds : oTable.selections
                        },
                        success:function(data){
                            if(data.status == 1){
                                layer.msg('删除成功');
                                $table.bootstrapTable('refresh');
                            }else{
                                layer.msg('删除失败');
                            }
                        }
                    })
                }else{
                    $.post({
                        url:"{:U('delWork')}",
                        data:{
                            selectedIds : oTable.workSelections
                        },
                        success:function(data){
                            if(data.status == 1){
                                layer.msg('删除成功');
                                $workTable.bootstrapTable('refresh');
                            }else{
                                layer.msg('删除失败');
                            }
                        }
                    })
                }

            })

        });


    </script>
</block>